<?php
/*
Plugin Name: Picasa Image Express
Plugin URI: http://psytoy.net/picasa-image-express
Description: Browse, search and select photos from any publicly available Picasa Web Album and add them to your post/pages.
Version: 1.1
Author: Scrawl
Author URI: http://psytoy.net

Copyright 2008 Scrawl  (scrawl@psytoy.net) 

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

Multi-language support 2009-3-24 by zern(zeroiszen@gmail.com)
URL http://www.cisvi.com/bo/picasa-image-express-965/
*/

// Plugin directory URI
$pluginURI = get_option('siteurl').'/wp-content/plugins/'.dirname(plugin_basename(__FILE__));

// -----------------------------------------------
// Define hooks and actions
// -----------------------------------------------

// Hook for adding admin menus
add_action('admin_menu', 'pie_add_settings_page');
if ((get_option("pie_hs_select") == "iframe")||(get_option("pie_hs_select") == "full")){
if ( in_array( $pagenow, array('post.php', 'post-new.php', 'page.php', 'page-new.php') ) ) {
	add_action( 'admin_head', 'pie_addStyleSheet' );
	add_action( 'admin_head', 'highslide_js' );
}}

// Hook for plugin activation
register_activation_hook( __FILE__, 'pie_init_options' );

// Hook for media button - late enough so that add_meta_box() is defined
if (is_admin()){
	add_action('media_buttons', 'pie_addMediaButton', 20);
}

// Attach stylesheet
//add_action('wp_head','pie_addStyleSheet');
//if (get_option("pie_hs_select")!= "none") {
//add_action( 'wp_footer', 'highslide_js' );
//}

// -----------------------------------------------
// Define plugin functions
// -----------------------------------------------
add_action('init', 'pie_textdomain');
function pie_textdomain() {
	load_plugin_textdomain('picasa-images-express', false, 'picasa-images-express');
}
// Add media button to editor
function pie_addMediaButton() {

	global $pluginURI;
	
	$media_picasa_iframe_src = "$pluginURI/album_viewer.php?";
	
	// Retrieve the settings from the database and append to popup window URL before thick box values
	$opt = pie_init_options();
	foreach ($opt as $key => $value){
		$media_picasa_iframe_src .= "&amp;".$key."=".$value;
	}
	$media_picasa_iframe_src .= "&TB_iframe=true&amp;height=500&amp;width=750";
	$media_picasa_title = __('Add Picasa image','picasa-images-express');
	if (($opt["pie_hs_select"] == "iframe")||($opt["pie_hs_select"] == "full")){
		echo "<a href=\"$media_picasa_iframe_src\" onclick=\"return hs.htmlExpand(this, { objectType: 'iframe',objectWidth:700,objectHeight:500,width:700,height:500} )\" title=\"$media_picasa_title\"><img src=\"$pluginURI/icon_picasa{$opt["pie_icon"]}.gif\" alt=\"$media_picasa_title\" /></a>";
		}else{
	echo "<a href=\"$media_picasa_iframe_src\" class=\"thickbox\" title=\"$media_picasa_title\"><img src=\"$pluginURI/icon_picasa{$opt["pie_icon"]}.gif\" alt=\"$media_picasa_title\" /></a>";
	}
}
function highslide_js() {
	global $pluginURI;
	$hsjs=get_option("pie_hs_select");
	// Add JS
			echo "<script type=\"text/javascript\" src=\"$pluginURI/highslide/highslide-$hsjs.js\"></script>
			<script type=\"text/javascript\">
			hs.graphicsDir =\"$pluginURI/highslide/graphics/\";
			hs.showCredits = false;
			hs.align = \"center\";\n";
			if (!is_admin()){
			if($hsjs=="gallery" || $hsjs=="full"){
			echo "hs.transitions = ['expand', 'crossfade'];
			hs.fadeInOut = true;
			hs.dimmingOpacity = 0.8;
			hs.outlineType = 'rounded-white';
			hs.captionEval = 'this.thumb.alt';
			hs.marginBottom = 90 // make room for the thumbstrip and the controls
			hs.numberPosition = 'caption';
			hs.addSlideshow({
				interval: 5000,
				repeat: false,
				useControls: true,
				overlayOptions: {
					className: 'text-controls',
					position: 'bottom center',
					relativeTo: 'viewport',
					offsetY: -75
		
				},
				thumbstrip: {
					position: 'bottom center',
					mode: 'horizontal',
					relativeTo: 'viewport'
				}
	});\n";}else{
		echo "hs.wrapperClassName =\"borderless floating-caption\";
			hs.registerOverlay({
			html: '<div class=\"closebutton\" onclick=\"return hs.close(this)\" title=\"Close\"></div>',
			overlayId: \"closebutton\",
			position: \"top right\",
			fade: 2 
			});\n";
		}}
			echo "</script>\n";
}

// Add a page which will hold the  Options form
function pie_add_settings_page() {
    //add_options_page('Test Options', 'Test Options', 8, 'testoptions', 'mt_options_page');
	add_options_page('Picasa Image Express', 'Picasa Image Express', 8, 'picasaImageExpress', 'pie_settings_form');
}

// Retrieve plugin options from database or set defaults if they don't exist
function pie_init_options(){

	//Define default values
	pie_textdomain();
	$pieOptions = array(
		"pie_user_name" => "psytoy78",
		"pie_hs_select"=>"image",
		"pie_link_thumbnails" => "true",
		"pie_thumb_size" => 160,
		"pie_square_thumbnails" => "true",
		"pie_image_size" => 640,
		"pie_link_option" => "picasa",
		"pie_captions" => "false",
		"pie_thumbnail_css" => "none",
		"pie_gallery_css" => "alignGalleryLeft",
		"pie_margin_top" => 10,
		"pie_margin_right" => 10,
		"pie_margin_bottom" => 10,
		"pie_margin_left" => 10,
		"pie_configured" => true,
		"pie_icon" => 1);
		
	// Apply defaults if not found
	if(!get_option("pie_configured")){
		foreach ($pieOptions as $key => $option){
			update_option($key,$option);
		}
	// Otherwise over-ride defaults with options stored in database
	} else {
		foreach ($pieOptions as $key => $option){
			$pieOptions[$key] = get_option($key);
		}
	}
	return $pieOptions;
}
// Attach stylesheet
function pie_addStyleSheet() {
	global $pluginURI;
	echo "<link href=\"$pluginURI/picasa-image-express.css\" rel=\"stylesheet\" type=\"text/css\" />\n";
	if (get_option("pie_hs_select")!= "none") {
	echo "<link href=\"$pluginURI/highslide/highslide.css\" rel=\"stylesheet\" type=\"text/css\" />\n";
		if (get_option("pie_hs_select")=="gallery" || get_option("pie_hs_select")=="full"){
	echo "<!--[if lt IE 7]>\n<link href=\"$pluginURI/highslide/highslide-ie6.css\" rel=\"stylesheet\" type=\"text/css\" />\n<![endif]-->\n";
	}
	echo "<style type=\"text/css\" media=\"screen\"> 
	.closebutton {
	/* For IE6, remove background and add filter */
	_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='$pluginURI/highslide/graphics/close.png', sizingMethod='scale');}
	</style>\n";
	}
}
// Settings page form
function pie_settings_form(){
	global $pluginURI;
?>
	<div class="wrap">
		<?php 
			// Retrieve the settings from the database
			$opt = pie_init_options();
			
			// debug assistant
			/*foreach ($opt as $key => $option){
				echo $key . "=" . $option . "<br/>";
			}*/
		?>

		<form method="post" action="options.php">
			<!-- Begin PIE form contents -->
			<?php printf(__('<h3>Hints and tips</h3><ul><li>All of the settings below are also available when selecting photos to insert.</li><li>The only item you need to setup on this page is the Picasa User Name. Everything else is optional.</li><li>To insert an image gallery, select multiple images using CTRL-Click and SHIFT-Click </li><li>The order you select images will determine the order they appear on your page or post</li></ul>','picasa-images-express')) ?>
			<h3><?php printf(__('Account setup','picasa-images-express')) ?>
			<table class="form-table">
				<tr>
					<th scope="row"><?php printf(__('Picasa User (<span style="color:red;">required</span>)','picasa-images-express')) ?></th>
					<td><input type="text" name="pie_user_name" size="40" value="<?php echo $opt['pie_user_name']; ?>" />
						<br />
<?php printf(__('Your Picasa web albums user name. Eg: <a href="http://picasaweb.google.com/psytoy78" target="_blank">http://picasaweb.google.com/<strong>psytoy78</strong></a>','picasa-images-express')) ?></td>
				</tr>
			</table>
			<h3><?php printf(__('Image setup','picasa-images-express')) ?></h3>
			<table class="form-table">
            	<tr>
					<th scope="row"><?php printf(__('Highslide JS Select:','picasa-images-express')) ?></th>
					<td><label>
						<input type="radio" name="pie_hs_select" value="none" <?php if ($opt["pie_hs_select"] == "none") { _e('checked="checked"'); } ?>  />
						<?php printf(__('Not use Highslide JS','picasa-images-express')) ?></label>
                        <label>
						<input type="radio" name="pie_hs_select" value="image" <?php if ($opt["pie_hs_select"]=="" || $opt["pie_hs_select"] == "image") { _e('checked="checked"'); } ?>  />
						<?php printf(__('Image','picasa-images-express')) ?></label>
						<label>
						<input type="radio" name="pie_hs_select" value="gallery" <?php if ($opt["pie_hs_select"] == "gallery") { _e('checked="checked"'); } ?> />
                        <?php printf(__('Gallery','picasa-images-express')) ?></label>
                        <label>
						<input type="radio" name="pie_hs_select" value="iframe" <?php if ($opt["pie_hs_select"] == "iframe") { _e('checked="checked"'); } ?> />
                        <?php printf(__('Iframe','picasa-images-express')) ?></label>
                         <label>
						<input type="radio" name="pie_hs_select" value="full" <?php if ($opt["pie_hs_select"] == "full") { _e('checked="checked"'); } ?> />
                        <?php printf(__('Full','picasa-images-express')) ?></label>
						<br />
                        <?php printf(__('Select Image to enable clicking on a photo to view a larger version(JS file size:24.6 kB)<br/>Select Gallery to enable Gallery base on Image(JS file size:35.6 kB)<br/>Select Iframe to enable Iframe content base on Image(JS file size:31.7 kB)<br/>Select Full to enable all(JS file size:41.3 kB)','picasa-images-express')) ?>
						</td>
				</tr>
				<tr>
					<th scope="row"><?php printf(__('Link to larger photo?','picasa-images-express')) ?></th>
					<td><label>
						<input type="radio" name="pie_link_thumbnails" value="true" <?php if ($opt["pie_link_thumbnails"] == "true") { _e('checked="checked"'); } ?>  />
						<?php printf(__('Yes','picasa-images-express')) ?></label>
						<label>
						<input type="radio" name="pie_link_thumbnails" value="false" <?php if ($opt["pie_link_thumbnails"] == "false") { _e('checked="checked"'); } ?> />
                        <?php printf(__('No','picasa-images-express')) ?></label>
						<br />
                        <?php printf(__('Select YES to enable clicking on a photo to view a larger version','picasa-images-express')) ?>
						</td>
				</tr>
				<tr>
					<th scope="row"><?php printf(__('Thumbnail size','picasa-images-express')) ?></th>
					<td><select name="pie_thumb_size">
							<option <?php if ($opt["pie_thumb_size"] == 32) { _e('selected="selected"'); } ?> >32</option>
							<option <?php if ($opt["pie_thumb_size"] == 48) { _e('selected="selected"'); } ?> >48</option>
							<option <?php if ($opt["pie_thumb_size"] == 64) { _e('selected="selected"'); } ?> >64</option>
							<option <?php if ($opt["pie_thumb_size"] == 72) { _e('selected="selected"'); } ?> >72</option>
							<option <?php if ($opt["pie_thumb_size"] == 144) { _e('selected="selected"'); } ?> >144</option>
							<option <?php if ($opt["pie_thumb_size"] == 160) { _e('selected="selected"'); } ?> >160</option>
							<option <?php if ($opt["pie_thumb_size"] == 200) { _e('selected="selected"'); } ?> >200</option>
							<option <?php if ($opt["pie_thumb_size"] == 288) { _e('selected="selected"'); } ?> >288</option>
							<option <?php if ($opt["pie_thumb_size"] == 320) { _e('selected="selected"'); } ?> >320</option>
							<option <?php if ($opt["pie_thumb_size"] == 400) { _e('selected="selected"'); } ?> >400</option>
							<option <?php if ($opt["pie_thumb_size"] == 512) { _e('selected="selected"'); } ?> >512</option>
							<option <?php if ($opt["pie_thumb_size"] == 576) { _e('selected="selected"'); } ?> >576</option>
							<option <?php if ($opt["pie_thumb_size"] == 640) { _e('selected="selected"'); } ?> >640</option>
							<option <?php if ($opt["pie_thumb_size"] == 720) { _e('selected="selected"'); } ?> >720</option>
							<option <?php if ($opt["pie_thumb_size"] == 800) { _e('selected="selected"'); } ?> >800</option>
						</select>
						<br />
                        <?php printf(__('The size of the photo to appear in your post/page. The longest side (ie width or height) will be this size','picasa-images-express')) ?></td>
				</tr>
				<tr>
					<th scope="row"><?php printf(__('Crop 1:1?','picasa-images-express')) ?></th>
					<td><label>
							<input type="radio" name="pie_square_thumbnails" value="true" <?php if ($opt["pie_square_thumbnails"] == "true") { _e('checked="checked"'); } ?> />
							<?php printf(__('Yes','picasa-images-express')) ?>
						</label>
						<label>
							<input type="radio" name="pie_square_thumbnails" value="false" <?php if ($opt["pie_square_thumbnails"] == "false") { _e('checked="checked"'); } ?> /><?php printf(__('No','picasa-images-express')) ?>
						</label><br />
                        <?php printf(__('This will crop photos so that they appear square. Useful when displaying a gallery of images so they are all the same size.<br />If set to YES, <strong>please ensure Thumbnail size is no larger than 160</strong>','picasa-images-express')) ?>
						</td>
				</tr>
				<tr>
					<th scope="row"><?php printf(__('Display captions?','picasa-images-express')) ?></th>
					<td><label><input type="radio" name="pie_captions" value="true" <?php if ($opt["pie_captions"] == "true") { _e('checked="checked"'); } ?> />
						<?php printf(__('Yes','picasa-images-express')) ?>
						</label>
						<label>
						<input type="radio" name="pie_captions" value="false" <?php if ($opt["pie_captions"] == "false") { _e('checked="checked"'); } ?> />
						<?php printf(__('No','picasa-images-express')) ?></label>
						<br />
                        <?php printf(__('Display a caption (if available) underneath the photo.<br/>Add or edit your captions by signing in to your <a href="http://picasaweb.google.com" target="_blank">Picasa Web Album account</a>','picasa-images-express')) ?></td>
				</tr>
				<tr>
					<th scope="row"><?php printf(__('Large image size','picasa-images-express')) ?></th>
					<td><select name="pie_image_size" >
							<option <?php if ($opt["pie_image_size"] == 32) { _e('selected="selected"'); } ?> >32</option>
							<option <?php if ($opt["pie_image_size"] == 48) { _e('selected="selected"'); } ?> >48</option>
							<option <?php if ($opt["pie_image_size"] == 64) { _e('selected="selected"'); } ?> >64</option>
							<option <?php if ($opt["pie_image_size"] == 72) { _e('selected="selected"'); } ?> >72</option>
							<option <?php if ($opt["pie_image_size"] == 144) { _e('selected="selected"'); } ?> >144</option>
							<option <?php if ($opt["pie_image_size"] == 160) { _e('selected="selected"'); } ?> >160</option>
							<option <?php if ($opt["pie_image_size"] == 200) { _e('selected="selected"'); } ?> >200</option>
							<option <?php if ($opt["pie_thumb_size"] == 288) { _e('selected="selected"'); } ?> >288</option>
							<option <?php if ($opt["pie_thumb_size"] == 320) { _e('selected="selected"'); } ?> >320</option>
							<option <?php if ($opt["pie_thumb_size"] == 400) { _e('selected="selected"'); } ?> >400</option>
							<option <?php if ($opt["pie_image_size"] == 512) { _e('selected="selected"'); } ?> >512</option>
							<option <?php if ($opt["pie_image_size"] == 576) { _e('selected="selected"'); } ?> >576</option>
							<option <?php if ($opt["pie_image_size"] == 640) { _e('selected="selected"'); } ?> >640</option>
							<option <?php if ($opt["pie_image_size"] == 720) { _e('selected="selected"'); } ?> >720</option>
							<option <?php if ($opt["pie_image_size"] == 800) { _e('selected="selected"'); } ?> >800</option>
						</select>
						<br />
                        <?php printf(__('The  maximum size of a photo that is linked from a thumbnail. The longest side (ie width or height) will be this size','picasa-images-express')) ?></td>
				</tr>
				<tr>
					<th scope="row"><?php printf(__('Open as','picasa-images-express')) ?></th>
					<td><label><input type="radio" name="pie_link_option" id="direct" value="direct" <?php if ($opt["pie_link_option"] == "direct") { _e('checked="checked"'); } ?> /><?php printf(__('Direct link','picasa-images-express')) ?></label>
						<label><input type="radio" name="pie_link_option" id="picasa" value="picasa" <?php if ($opt["pie_link_option"] == "picasa") { _e('checked="checked"'); } ?> /><?php printf(__('Picasa','picasa-images-express')) ?></label>
						<label><input type="radio" name="pie_link_option" id="lightbox" value="lightbox" <?php if ($opt["pie_link_option"] == "lightbox") { _e('checked="checked"'); } ?> /><?php printf(__('Lightbox','picasa-images-express')) ?></label>
                        						<label><input type="radio" name="pie_link_option" id="highslide" value="highslide" <?php if ($opt["pie_link_option"] == "highslide") { _e('checked="checked"'); } ?> /><?php printf(__('Highslide','picasa-images-express')) ?></label>
						<br />
                        <?php printf(__('Direct link - link directly to photo<br />Picasa - link to  Picasa Web Album<br />Lightbox - link to photo and add support for a third-party <a href="http://wordpress.org/extend/plugins/slimbox-plugin/" title="My personal favourite" target="_blank">Lightbox</a> plugin<br />Highslide - this plugin support','picasa-images-express')) ?>
						</label></td>
				</tr>
			</table>
			<h3><?php printf(__('Styles and formatting','picasa-images-express')) ?></h3>
			<table class="form-table">
				<tr>
					<th scope="row"><?php printf(__('Alignment of a single image','picasa-images-express')) ?></th>
					<td><select name="pie_thumbnail_css">
							<option <?php if ($opt["pie_thumbnail_css"] == "none") { _e('selected="selected"'); } ?> value="none" ><?php printf(__('none','picasa-images-express')) ?></option>
							<option <?php if ($opt["pie_thumbnail_css"] == "alignleft") { _e('selected="selected"'); } ?> value="alignleft" ><?php printf(__('left','picasa-images-express')) ?></option>
							<option <?php if ($opt["pie_thumbnail_css"] == "alignright") { _e('selected="selected"'); } ?> value="alignright" ><?php printf(__('right','picasa-images-express')) ?></option>
						</select>
						<br />
                        <?php printf(__('Alignment of a single image relative to text.','picasa-images-express')) ?></td>
				</tr>
				<tr>
					<th scope="row"><?php printf(__('Alignment of a gallery of images','picasa-images-express')) ?></th>
					<td><select name="pie_gallery_css">
							<option <?php if ($opt["pie_gallery_css"] == "none") { _e('selected="selected"'); } ?> value="none"><?php printf(__('none','picasa-images-express')) ?></option>
							<option <?php if ($opt["pie_gallery_css"] == "alignGalleryLeft") { _e('selected="selected"'); } ?> value="alignGalleryLeft"  ><?php printf(__('left','picasa-images-express')) ?></option>
							<option <?php if ($opt["pie_gallery_css"] == "alignGalleryRight") { _e('selected="selected"'); } ?> value="alignGalleryRight" ><?php printf(__('right','picasa-images-express')) ?></option>
							<option <?php if ($opt["pie_gallery_css"] == "alignGalleryCenter") { _e('selected="selected"'); } ?> value="alignGalleryCenter" ><?php printf(__('center','picasa-images-express')) ?></option>
						</select>
						<br /><?php printf(__('Alignment of images in a gallery relative to each other.','picasa-images-express')) ?></td>
				</tr>
				<tr>
					<th scope="row"><?php printf(__('Margins','picasa-images-express')) ?></th>
					<td><label><?php printf(__('Top:','picasa-images-express')) ?>
							<input name="pie_margin_top" type="text" size="5" value="<?php echo $opt['pie_margin_top']; ?>" />
						</label>
						<label><?php printf(__('Right:','picasa-images-express')) ?>
							<input name="pie_margin_right" type="text" size="5" value="<?php echo $opt['pie_margin_right']; ?>" />
						</label>
						<label><?php printf(__('Bottom:','picasa-images-express')) ?>
							<input name="pie_margin_bottom" type="text" size="5" value="<?php echo $opt['pie_margin_bottom']; ?>" />
						</label>
						<label><?php printf(__('Left:','picasa-images-express')) ?>
							<input name="pie_margin_left" type="text" size="5" value="<?php echo $opt['pie_margin_left']; ?>" />
						</label>
						<br/><?php printf(__('Amount of space surrounding an image','picasa-images-express')) ?>
					</td>
				</tr>
			</table>		
			<h3><?php printf(__('Misc settings','picasa-images-express')) ?></h3>
			<table class="form-table">
				<tr>
					<th scope="row"><?php printf(__('Insert Picasa icon','picasa-images-express')) ?></th>
					<td>
						<label>
						<input type="radio" name="pie_icon" value="1" <?php if ($opt["pie_icon"] == "1") { _e('checked="checked"'); } ?>  />
						<img src="<?php echo $pluginURI; ?>/icon_picasa1.gif" alt="Option 1" title="Option 1"/></label>
						<label>
						<input type="radio" name="pie_icon" value="2" <?php if ($opt["pie_icon"] == "2") { _e('checked="checked"'); } ?> />
						<img src="<?php echo $pluginURI; ?>/icon_picasa2.gif" alt="Option 2" title="Option 2"/></label>
						<label>
						<input type="radio" name="pie_icon" value="3" <?php if ($opt["pie_icon"] == "3") { _e('checked="checked"'); } ?> />
						<img src="<?php echo $pluginURI; ?>/icon_picasa3.gif" alt="Option 3" title="Option 3"/></label>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php printf(__('Change all settings back to defaults','picasa-images-express')) ?></th>
					<td>
						<label>
						<input type="radio" name="pie_configured" value="0" <?php if (!$opt["pie_configured"]) { _e('checked="checked"'); } ?>  />
						<?php printf(__('Yes','picasa-images-express')) ?></label>
						<label>
						<input type="radio" name="pie_configured" value="1" <?php if ($opt["pie_configured"]) { _e('checked="checked"'); } ?> />
						<?php printf(__('No','picasa-images-express')) ?></label>
					</td>
				</tr>
			</table>
			<!-- End PIE form contents -->
			
			<!-- Begin Wordpress required fields -->
			<?php wp_nonce_field('update-options'); ?>
			<input type="hidden" name="action" value="update" />
			<input type="hidden" name="page_options" value="pie_user_name,pie_hs_select,pie_link_thumbnails,pie_thumb_size,pie_square_thumbnails,pie_image_size,pie_link_option,pie_captions,pie_thumbnail_css,pie_gallery_css,pie_margin_top,pie_margin_right,pie_margin_bottom,pie_margin_left,pie_configured,pie_icon" />
			<p class="submit">
				<input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />
			</p>
			<!-- End Wordpress required form elements -->
		</form>
	</div>
<?php
}
?>